﻿using System;
using System.Collections.Generic;
using Treely.Data.Model.Abstract;

namespace Treely.Data.Model
{
    public static class QuizFactory
    {
        private static Random RandomNumber = new Random(DateTime.Now.Millisecond);

        public static List<IAnswerable> MakeDefaultQuestions(DifficultyLevel level, int? numberOfQuestions = null)
        {
            var quizLength = (numberOfQuestions == null || numberOfQuestions == 0)
                ? DefaultLengthByLevel(level)
                : numberOfQuestions;
            var list = new List<IAnswerable>();
            for (var i = 0; i < quizLength; i++)
            {
                list.Add(MakeQuestion(level, (int)level > 1, (int)level > 4, (int)level > 5));
            }

            return list;
        }

        public static IAnswerable MakeQuestion(DifficultyLevel level, bool includeSubtraction = false, bool includeMultiplication = false, bool includeDivision = false)
        {
            var chance = RandomNumber.Next(0, 100);
            if (includeSubtraction && chance >= 0 && chance <= 60)
                return new SubtractionQuestion(level, RandomNumber);
            if (includeMultiplication && chance > 50 && chance < 76)
                return new MultiplicationQuestion(level, RandomNumber);
            if (includeMultiplication && chance > 75)
                return new DivisionQuestion(level, RandomNumber);
            return new AdditionQuestion(level, RandomNumber);
        }

        public static int DefaultLengthByLevel(DifficultyLevel level)
        {
            switch (level)
            {
                case DifficultyLevel.Preschool:
                    return 5;

                case DifficultyLevel.Kindergarten:
                    return 15;

                case DifficultyLevel.FirstGrade:
                    return 20;

                case DifficultyLevel.SecondGrade:
                    return 20;

                case DifficultyLevel.ThirdGrade:
                case DifficultyLevel.FourthGrade:
                    return 50;

                default:
                    return 3;
            }
        }
    }
}